package com.qfmy.mystation.ui.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.qfmy.mystation.R;
import com.qfmy.mystation.bean.Song;
import com.qfmy.mystation.listener.OnRecyclerViewItemClickListener;
import com.qfmy.mystation.utils.DateTimeUtils;

import java.util.List;

public class MusicListAdapter extends RecyclerView.Adapter<MusicListAdapter.MyViewHolder> {
    private Context mContext;
    private List<Song> songList;
    private OnRecyclerViewItemClickListener mListener;

    public MusicListAdapter(Context mContext, List<Song> songList) {
        this.mContext = mContext;
        this.songList = songList;
    }

    public void setRecyclerViewItemClickListener(OnRecyclerViewItemClickListener mListener) {
        this.mListener = mListener;
    }

    /**
     * 刷新数据
     */
    @SuppressLint("NotifyDataSetChanged")
    public void changeState() {
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new MyViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_music_rv_list, parent, false));
    }

    @SuppressLint({"SetTextI18n", "ResourceAsColor"})
    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, @SuppressLint("RecyclerView") int position) {
        Song item = songList.get(position);
        int duration = item.duration;
        String time = DateTimeUtils.formatTime(duration);

        //歌曲名称
        holder.tv_song_name.setText(item.getSong().trim());
        holder.tv_singer.setText(item.getSinger().trim());
        holder.tv_duration_time.setText(time);
        holder.tv_position.setText(position + 1 + "");
        holder.item_music.setOnClickListener(view -> mListener.onItemClick(view, position));
    }

    @Override
    public int getItemCount() {
        return songList == null ? 0 : songList.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        TextView tv_position;
        TextView tv_song_name;
        TextView tv_singer;
        TextView tv_duration_time;
        LinearLayout item_music;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            tv_position = itemView.findViewById(R.id.tv_position);
            tv_song_name = itemView.findViewById(R.id.tv_song_name);
            tv_singer = itemView.findViewById(R.id.tv_singer);
            tv_duration_time = itemView.findViewById(R.id.tv_duration_time);
            item_music = itemView.findViewById(R.id.item_music);
        }
    }
}
